package io.jft.doll.model.viewModel

import android.annotation.SuppressLint
import android.app.AlertDialog
import com.g.base.extend.dp
import com.g.base.extend.observeExOnce
import com.g.base.room.repository.NetworkBoundResult
import com.g.base.room.repository.Status
import com.g.base.ui.BaseActivity
import com.g.base.viewModel.BaseViewModel
import io.jft.doll.api.UserApi
import io.jft.doll.api.apiProvider
import io.jft.doll.databinding.DialogDailyRewardBinding
import io.jft.doll.room.repository.AdRepository

/**
 * Created by G on 2017/11/29 0029.
 */
class HomeViewModel : BaseViewModel() {
    val roomLimit = 20

    private val adRepository by lazy { AdRepository() }
    /**
     * 获取广告区域
     */
    fun getBanner() = adRepository.getMainBanner()

    @SuppressLint("CheckResult")
    fun dailyReward(activity: BaseActivity<*>) {
        NetworkBoundResult(
                {
                    apiProvider.create(UserApi::class.java)
                            .dailyReward()
                }
        ).observeExOnce(activity) {
            if (it.status == Status.Content) {
                val view = DialogDailyRewardBinding.inflate(activity.layoutInflater)!!
                view.reward = "+${it.data}"
                val dialog = AlertDialog.Builder(activity).setCancelable(true).setView(view.root).show()
                dialog.window.setBackgroundDrawable(null)
                val lp = dialog.window.attributes
                lp.width = 300.dp()
                lp.height = 234.dp()
                dialog.window.attributes = lp
                view.root.setOnClickListener {
                    dialog.dismiss()
                }
            }
        }
    }
}